﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Text;
using SocialIn.Core.Data;
using System.Data;
using SocialIn.Biz;

namespace SocialIn.Web.Search
{
    public partial class VolunteerSearch : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitSetting();

                this.htTotalPage.Value = TotalItemCount(null, null).ToString();

                this.boardItemList.InnerHtml = ItemRead(1, null, null);
            }
        }

        private void InitSetting()
        {
            this.ddlLocation.DataValueField = "SubCategory";
            this.ddlLocation.DataTextField = "CodeName";

            this.ddlTalent.DataValueField = "SubCategory";
            this.ddlTalent.DataTextField = "CodeName";

            CommonData common = new CommonData();
            DataTable result = common.GetCodeMaster("M004").ReturnValue;
            DataRow row = result.NewRow();
            row["SubCategory"] = "-1";
            row["CodeName"] = "전체 지역";
            result.Rows.InsertAt(row, 0);

            ddlLocation.DataSource = result;
            ddlLocation.DataBind();

            result = common.GetCodeMaster("M003").ReturnValue;
            row = result.NewRow();
            row["SubCategory"] = "-1";
            row["CodeName"] = "전체 재능";
            result.Rows.InsertAt(row, 0);

            ddlTalent.DataSource = result;
            ddlTalent.DataBind();
        }

        [WebMethod]
        public static string ItemRead(int currentIndex, string location, string talent)
        {
            int pageSize = 7;

            StringBuilder html = new StringBuilder(@"<table cellspacing='0' class='ResultTable'>");

            Volunteer vol = new Volunteer();
            Result<DataTable> resultVolunteer = vol.SearchVolunteer(pageSize, currentIndex, location, talent);
            Result<DataTable> resultMyTalent = vol.SearchMyTalent();

            if (resultVolunteer.ReturnValue == null || resultVolunteer.ReturnValue.Rows.Count == 0)
            {
                html.Append("<tr><td colspan='3'>조회된 드리미가 없습니다.</td></tr>");
            }
            else
            {
                foreach (DataRow item in resultVolunteer.ReturnValue.Rows)
                {
                    string locationString = item["Location"].ToString();
                    if (string.IsNullOrEmpty(locationString) == true)
                        locationString = "선호하는 봉사장소 미정";

                    html.Append("<tr>");

                    DataRow[] talents = resultMyTalent.ReturnValue.Select(String.Format("UserID = '{0}'", item["UserID"]));

                    if (talents == null || talents.Length == 0)
                    {
                        html.Append("<img alt='' src=''/>");
                    }
                    else
                    {
                        html.Append("<td>");
                        DataRow talentItem = talents[0] as DataRow;
                        html.AppendFormat("<img alt='[{0}]{1}' src='{2}' />"
                            , talentItem["TalentName"], talentItem["Desc"]
                            , VirtualPathUtility.ToAbsolute(talentItem["ImagePath"].ToString()));
                        //, HttpContext.Current.Server. .MapPath(temp["ImagePath"].ToString()));

                        html.Append("</td>");

                    }

                    html.AppendFormat("<td>{0} / {1}<br>{2}", locationString, item["UserName"], item["SelfIntroduction"]);
                    html.Append("</tr>");
                }
            }

            html.Append("</table>");

            return html.ToString();
        }

        [WebMethod]
        public static int TotalItemCount(string location, string talent)
        {
            Volunteer vol = new Volunteer();
            return vol.GetVolunteerTotalCount(7, location, talent);
        }
    }
}